package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.common.ArticleClassAddDto;
import com.dog.basics.bean.dto.admin.common.ArticleClassEditDto;
import com.dog.basics.bean.dto.admin.common.ArticleClassFormDto;
import com.dog.basics.bean.model.common.DogComArticle;
import com.dog.basics.bean.model.common.DogComArticleClass;
import com.dog.basics.bean.vo.admin.common.PcArticleClassVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.common.DogComArticleClassMapper;
import com.dog.basics.dao.common.DogComArticleMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.IDogComArticleClassService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * cms文章分类表 服务实现类
 * </p>
 *
 * @author guonh
 * @since 2020-05-20
 */
@Service
@AllArgsConstructor
public class DogComArticleClassServiceImpl extends ServiceImpl<DogComArticleClassMapper, DogComArticleClass> implements IDogComArticleClassService {
    private final DogComArticleMapper dogComArticleMapper;

    /**
     * @Description: 增加cms文章分类
     * @Param:
     * @return: Boolean
     * @Author: zhangrm
     * @Date: 2020/05/22  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveObj(ArticleClassAddDto obj) {
        Integer count = this.getBaseMapper().selectCount(Wrappers.<DogComArticleClass>lambdaQuery()
                .eq(DogComArticleClass::getName, obj.getName()));
        Validator.validateTrue(count.equals(0), "cms文章分类名称已经存在!");
        DogComArticleClass dogComArticleClass = new DogComArticleClass();
        BeanUtil.copyProperties(obj, dogComArticleClass);
        dogComArticleClass.setCreateTime(DateUtil.currentSeconds());
        dogComArticleClass.setCreateBy(SecurityUtils.getSysUser().getUserId());
        Validator.validateTrue(this.save(dogComArticleClass), "增加cms文章分类失败!");
        return Boolean.TRUE;
    }

    /**
     * @Description: 删除cms文章分类
     * @Param: ids
     * @return: Boolean
     * @Author: zhangrm
     * @Date: 2020/05/22  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(Long id) {
        DogComArticleClass dogComArticleClass = this.getById(id);
        Validator.validateNotNull(dogComArticleClass, "无此数据");
        //判断有无下级
        List<DogComArticle> clist =  dogComArticleMapper.selectList(Wrappers.<DogComArticle>lambdaQuery().eq(DogComArticle::getClassId,id));
        Validator.validateTrue(CollUtil.isEmpty(clist), "文章分类下有文章不能删除!");
        List<DogComArticleClass> list = this.list(Wrappers.<DogComArticleClass>lambdaQuery().eq(DogComArticleClass::getPid, id));
        Validator.validateTrue(CollUtil.isEmpty(list), "文章分类下有下级文章分类不能删除!");
        return this.removeById(id);
    }


    /**
     * @Description: 修改cms文章分类
     * @Param:
     * @return: Boolean
     * @Author: zhangrm
     * @Date: 2020/05/22  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editObj(ArticleClassEditDto obj) {
        DogComArticleClass dogComArticleClass = this.getById(obj.getId());
        Validator.validateNotNull(dogComArticleClass, "无此数据");
        BeanUtil.copyProperties(obj, dogComArticleClass);
        dogComArticleClass.setUpdateTime(DateUtil.currentSeconds());
        dogComArticleClass.setUpdateBy(SecurityUtils.getSysUser().getUserId());
        return this.updateById(dogComArticleClass);
    }


    /**
     * @Description: 分页cms文章分类列表
     * @Param: status
     * @return: IPage<>
     * @Author: zhangmr
     * @Date:
     */
    @Override
    public IPage<PcArticleClassVo> getPageList(PageParam page, ArticleClassFormDto obj) {
        IPage iPage = this.page(page, Wrappers.<DogComArticleClass>lambdaQuery()
                .eq(ObjectUtil.isNotNull(obj.getId()), DogComArticleClass::getId, obj.getId())
                .eq(ObjectUtil.isNotNull(obj.getPid()), DogComArticleClass::getPid, obj.getPid())
                .like(StrUtil.isNotBlank(obj.getName()), DogComArticleClass::getName, obj.getName())
                .orderBy(Boolean.TRUE, obj.getIsOrderByAvg(), DogComArticleClass::getSort));
        List<DogComArticleClass> records = iPage.getRecords();
        iPage.setRecords(records.stream().map(e -> {
            PcArticleClassVo questionTypeVo = new PcArticleClassVo();
            BeanUtil.copyProperties(e, questionTypeVo);
            return questionTypeVo;
        }).collect(Collectors.toList()));
        return iPage;
    }
}
